package cn.design.realize;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author dlq
 * @description
 *            宏命令模式（组合命令模式）: 利用组合模式实现
 * @date 2020/9/27 13:44
 * @since 1.0
 */
public class MacroCommand  implements RemoteControlCommand{

    List<RemoteControlCommand> commands;

    public MacroCommand(List<RemoteControlCommand> commands) {
        this.commands = commands;
    }

    @Override
    public void execute() {
        //执行每条命令的 execute 方法
        commands.forEach(RemoteControlCommand::execute);
    }
    public List<RemoteControlCommand> add(RemoteControlCommand command){
        commands.add(command);
        return commands;
    }
    public void remove(RemoteControlCommand command){
      /*  Iterator<RemoteControlCommand> iterator = commands.iterator();
        while (iterator.hasNext()){
            if (command.equals(iterator.next())){
                iterator.remove();
            }

        }*/
        //上面的简化
        commands.removeIf(command::equals);

    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            String i = iterator.next();
            if (i.equals("3") || i.equals("5")){
                iterator.remove();
            }
        }
        list.forEach(System.out::println);
    }
}
